package org.yefei.homeworkday25;

import java.io.*;

public class Four {

	public static void main(String[] args) {
		// 4:获得文件行数的方法
		// 有 bufferedReader.readLine 方法 或者查找byte是否=='\n',比较谁快！
		InputStream i = null, f = null;
		Reader r = null;
		BufferedReader b = null;
		try {
			i = new FileInputStream(
					"D:\\jee-2018-12\\java\\Java61_homework\\src\\org\\yefei\\homeworkday25\\Four.java");
			r = new InputStreamReader(i, "gbk");
			b = new BufferedReader(r);

			int count = 0;// 计数
			b.skip(299);
			long start = System.nanoTime();
			while (b.readLine() != null) {
				count++;
			}
			long end = System.nanoTime();
			System.out.println("文件的行数是：" + count);
			System.out.println("方法一用时：" + (end - start) + "纳秒");

			f = new FileInputStream(
					"D:\\jee-2018-12\\java\\Java61_homework\\src\\org\\yefei\\homeworkday25\\Four.java");
			byte[] by = new byte[f.available()];
			int read = f.read(by);
			int count1 = 0;
			long start1 = System.nanoTime();
			for (int j = 0; j < read; j++) {
				if (by[j] == '\n') {
					count1++;
				}
			}
			long end1 = System.nanoTime();
			System.out.println("文件的行数是：" + (count1));
			System.out.println("方法二用时：" + (end1 - start1) + "纳秒");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (f != null)
				try {
					f.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			if (b != null)
				try {
					b.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (r != null)
				try {
					r.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (i != null)
				try {
					i.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}

	}

}
